const q = require('./q')
/**
 * source: true  返回本地路径
 * sourceType: 选取的类别
 * num: 最多上传的图片张数
 * isOne: 不是订单所要上传的图片
*/
function showActionSheet(args) {
    if (typeof args == 'undefined') {
        args = {}
    }
    return q.Promise((resolve, reject) => {
        wx.showActionSheet({
            itemList: ['拍照', '从手机相册选择'],
            success: function (res) {
                var sourceType = ['album', 'camera']
                if (res.tapIndex == 0) {
                    sourceType = ['camera']
                } else if(res.tapIndex == 1) {
                    sourceType = ['album']
                } else if(res.cancel) {
                    return
                }
                args.sourceType = sourceType
                chooseImage(args).then(data => {
                    resolve(data)
                })
            },
            fail: function (err) {
                console.log(err)
            }
        })
    })
}

function chooseImage(args) {
    return q.Promise((resolve, reject) => {
        wx.chooseImage({
            count: args.num || 1, // 默认9
            sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
            sourceType: args.sourceType, // 可以指定来源是相册还是相机，默认二者都有
            success: function (res) {
                // console.log(1)
                // debugger
                // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
                let tempFilePaths = res.tempFilePaths
                if (args.source) {
                    resolve(tempFilePaths)
                } else {
                    wx.setStorageSync('pic', tempFilePaths)
                    wx.redirectTo({
                        url: '/pages/edit/edit'
                     })
                    // uploadFile(tempFilePaths, args).then(data => {
                    //     resolve(data)
                    // })
                }
            }
        })
    })
}

function uploadFile(files, args) {
    let self = this
    let data = {
        pics: [],
        all_success: false
    }
    return q.Promise((resolve, reject) => {
        let upload = function (files) {
            let len = files.length - 1
            let file = files.pop()
            wx.uploadFile({
            url:"https://www.vaniswdq.com/springUpload",
                filePath: file,
                name:'file',
                success(res) {
                    wx.setStorageSync('pic', res.data)
                    wx.redirectTo({
                        url: '/pages/edit/edit'
                     })
                },
                fail(err) {
                    data.pics.unshift({ key: len, state: false })
                    data.all_success = true
                },
                complete(cc) {
                    console.log('uploadFile', cc)
                    if (files.length > 0) {
                        upload(files)
                    } else {
                        resolve(data)
                    }
                }
            })
        }
        upload(files)
    })
}

function previewImage(...args) {
    formatUrl(args).then(datas => {
        wx.previewImage({
            current: datas.src,
            urls: datas.urls
        })
    })
}

function formatUrl(args) {
    let datas = {
        src: args[1],
        urls: []
    }
    return q.Promise((resolve, reject) => {
        args[0].map(v => {
            datas.urls.push(v.image.url)
        })
        resolve(datas)
    })
}

module.exports = {
    showActionSheet: showActionSheet,
    chooseImage: chooseImage,
    uploadFile: uploadFile,
    previewImage: previewImage
}